<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0">
	
<title>商城首页</title>

<link href="../../public/css/base.css" rel="stylesheet">
<link href="../../public/css/shop.css" rel="stylesheet">
<link href="../../public/css/user.css" rel="stylesheet">
<script src="../../public/js/jquery.min.2.1.4.js"></script>
<script src="../../public/js/md5.js"></script>
<script src="../../public/js/sort_ascii.js"></script>
<script src="../../public/js/f_data.js"></script>
</head>

<body>
<header class="header">
	<div class="header-return">
	    <a href="../home/index.html"></a>
	</div>
	
	<div class="search_box">
		<input id="search" placeholder="请输入你要找的商品">
		<i class="clear-keyword"></i>
	</div>
	
	<div class="header-btn">
		<a href="shopcart.html" class="header-shopcart">
			<i>12</i>
		</a>
	</div>
</header>

<!--筛选-->
<div class="sort">
	<div class="sort-bar">
		<ul>
			<li id="openSort" class="active">
				<span>综合</span>
				<i></i>
				
				<div class="sort-box">
					<div class="sort-option">
						<ul>
							<li class="active">综合</li>

							<li>价格从低到高</li>

							<li>价格从高到低</li>

							<li>最新上架</li>
						</ul>
					</div>
				</div>
			</li>
			
			<li>
				<span>销量</span>
			</li>
			
			<li>
				<span>评价</span>
			</li>
		</ul>
	</div>
	
	<div class="sort-catalog">
		<a href="productCatalog.html">
			<i></i>
			<span>分类</span>
		</a>
	</div>
</div>

<section class="container shop-container">
	<div class="goods">
		
		<!--<div class="load-no-more">
			<span>没有更多了</span>
		</div>-->
    </div>
</section>
<div class="nav">
	<ul>
		<li id='pay'>
			<a href="../wallet/digitalassets.html" class="nav-assets">
				<i></i>
				<span>交易</span>
			</a>
		</li>
		<li id='shop'>
			<a href="../shop/shopindex.html" class="scan-btn">
				<i></i>
				<!--<span>扫码转账</span>-->
			</a>
		</li>
		<li id='zichan'>
			<a href="../home/index.html" class="nav-shop">
				<i></i>
				<span>资产</span>
			</a>
		</li>
	</ul>
</div>

<script>
	
	var g_u_ids = checktoken('uid','token');
	var g_u_id = getCookie('uid');
	var g_token = getCookie('token');
	var g_time = Date.parse(new Date())/1000;
	let g_datas = {g_u_id,g_token,g_time};
    let g_data = sort_ascii(g_datas);
	var g_code = sort_code(g_data);
	$.ajax({
	    type: 'post',
	    url: url+"/index.php/api/Shop/shop_index",
	    data:{u_id:g_u_id,token:g_token,time:g_time,code:g_code},
	    success: function (msg){
	    		if(msg.code == 1){
	    			// alert(msg.data.data);
	    			//遍历键值对
	    			$.each(msg.data.data,function(key,value){  
	    				var htmladd="";
	    					htmladd +=	'<div class="goods-box">'
					        htmladd +=    '<div class="goods-img">'
					        htmladd +=        '<img src="http://web.bai918.com'+value.img+'">'
					        htmladd +=    '</div>'
					        htmladd +=    '<h3>'+value.name+'</h3>'
							htmladd +=	  '<p>价值:'+value.money+'</p>'
							htmladd +=	  '<p>抢单时间:'+value.contract_time+'</p>'
							htmladd +=	  '<p>合约/收益:'+value.day+'/'+value.contract_shouyi+'</p>'
							htmladd +=	  '<p>预约:'+value.cost+'SYB</p>'
							htmladd +=      '<button class="yuyue" onclick="yy()">预约</button>'
			    			htmladd +=		'<button class="qiangdan" onclick="qd()">抢单</button>'
					    	htmladd +=	'</div>';
					    	$(".goods").append(htmladd);
				  	});
			  		// setCookie('token',msg.data.token,1);
				}
				if(msg.code == 0){
	    			location.href="../login/login.html"
				}
	    },
        error : function() {
	          	//提交失败的提示词或者其他反馈代码
	        	alert('失败！');
        }
	 });

	function yy() {
			// var g_u_id = $("input[name=mobile]").val();
			var category_id = 1;
			// var g_token = $("input[name=qpassword]").val();
			var time = Date.parse(new Date())/1000;
			
		    let category_datas = {g_u_id,category_id,g_token,time};
		    let category_data = sort_ascii(category_datas);
			var category_code = sort_code(category_data);
            $.ajax({
                type: "POST",//方法
                url: url+"/index.php/api/Shop/shop_yuyue" ,//表单接收url
                data: {u_id:g_u_id,token:g_token,code:category_code,time:time,category_id:category_id},
                success: function(msg) {
                  //提交成功的提示词或者其他反馈代码
					if(msg.code == 1){
						alert(msg.msg);
						
					}

					if(msg.code == 0){
						alert(msg.msg);
					}
                },
                error : function() {
                  //提交失败的提示词或者其他反馈代码
                    alert('失败！');
                }
            });
        };

	function qd() {
			// var g_u_id = $("input[name=mobile]").val();
			var category_id = 1;
			// var g_token = $("input[name=qpassword]").val();
			var time = Date.parse(new Date())/1000;
			
		    let category_datas = {g_u_id,category_id,g_token,time};
		    let category_data = sort_ascii(category_datas);
			var category_code = sort_code(category_data);
            $.ajax({
                type: "POST",//方法
                url: url+"/index.php/api/Shop/shop_qiangdan" ,//表单接收url
                data: {u_id:g_u_id,token:g_token,code:category_code,time:time,category_id:category_id},
                success: function(msg) {
                  //提交成功的提示词或者其他反馈代码
					if(msg.code == 1){
						alert(msg.msg);
						
					}

					if(msg.code == 0){
						alert(msg.msg);
					}
                },
                error : function() {
                  //提交失败的提示词或者其他反馈代码
                    alert('失败！');
                }
            });
        };

	$(function(){
		
		//搜索框清除
		$('#search').on('input',function(){
			 
			if(!($('#search').val()=='')){
				$('.clear-keyword').show();
			}else{
				$('.clear-keyword').hide();
			}
			 
		});
		
		$('.clear-keyword').click(function(){
			$('#search').val('');
			$(this).hide();
		});
		
		
		//筛选
		$('.sort-bar li').click(function(){
			$(this).addClass('active').siblings().removeClass('active');
			
			if( $(this).attr('id')=='openSort'){
				$(this).toggleClass('open');
				$('body').css({'width':'100%','height':'100%','overflow':'hidden'});
			}
			else{
				$('#openSort').removeClass('open');
				$('body').css({'width':'auto','height':'auto','overflow':'auto'});
			}
			
		});
		
		$('.sort-bar li .sort-box').click(function(){
			$('#openSort').removeClass('open');
			$('body').css({'width':'auto','height':'auto','overflow':'auto'});
		});

        /**
         * 抢单倒计时代码块
         * @author: leny
         * @date: 2020/7/6 10:03
         */
        var setH = 9,setM = 59,setS = 0;
        var countDown = false;
        setQdTime()
        function setQdTime(){
            var date = new Date();
            var h = date.getHours();
            var m = date.getMinutes();
            var s = date.getSeconds();
            //如果不需要抢单未开始文字 屏蔽if代码即可为隐藏抢单按钮
            if(h < setH || (h<=setH && m<=setM) || (h<=setH && m<=setM && s<=setS)){
                $(".qiangdan").css({display:'block'})
                $(".qiangdan").text('抢单未开始')
                $(".qiangdan").attr('disabled', true);
            }
            //打开页面的时候是9点59分59秒 需要保持精确度
            if(h == setH && m == setM && (60-s <= 1)) countTime();
            if(h > setH) setQdDom('start');

            //每隔1秒检测，保证系统性能
            const checkTime = setInterval(() => {
                countTime()
            }, 1000);

        }
        function countTime(){
            var date = new Date();
            var h = date.getHours();
            var m = date.getMinutes();
            var s = date.getSeconds();
            //保证是在9点59分执行倒计时
            if(h == setH && m == setM){
                if(!countDown ){
                    //每隔50毫秒倒计时，保证倒计时精确度
                    countDown = setInterval(()=>{
                        var date = new Date();
                        var m = date.getMinutes();
                        var s = date.getSeconds();
                        var surplusS = 60 - s;
                        if(m == setM) setQdDom('countdown',surplusS)
                        if(m != setM) {
                            setQdDom('start')
                            clearInterval(countDown);
                            countDown = false;
                        }
                    },50)
                }
            }
        }

        function setQdDom(type,surplusS){
            if(!type || type == undefined) return false;
            if(type=='countdown' && (surplusS == undefined)) return false;
            if(type=='countdown'){
                $(".qiangdan").css({display:'block'})
                $(".qiangdan").text('剩余'+surplusS+'秒')
                $(".qiangdan").attr('disabled', true);
            }
            if(type=='start'){
                $(".qiangdan").css({display:'block'})
                $(".qiangdan").text('抢单')
                $(".qiangdan").attr('disabled', false);
            }
        }
        //end
		
		//$('#openSort').click(function(){
//			$(this).addClass('open');
//			$('.sort-box').show();
//			
//			$('body').css({'width':'100%','height':'100%','overflow':'hidden'});
//		});
		
	})
</script>
</body>
</html>
